using System;
using System.Linq;
using System.Linq.Expressions;

namespace jSLCore.Service.Workflow.Linq.jProperty
{
    public class jPropertyProvider : IQueryProvider
    {
        private readonly IQueryProvider _provider;
        private jPropertyVisiter _jPropertyVisiter;

        public jPropertyProvider(IQueryProvider provider)
        {
            _provider = provider;
        }

        public IQueryable CreateQuery(Expression expression)
        {
            _jPropertyVisiter = new jPropertyVisiter();
            expression = _jPropertyVisiter.Visit(expression);
            var queryable = _provider.CreateQuery(expression);
            return queryable;
        }

        public IQueryable<TElement> CreateQuery<TElement>(Expression expression)
        {
            return (IQueryable<TElement>) CreateQuery(expression);
        }

        public object Execute(Expression expression)
        {
            return _provider.Execute(expression);
        }

        public TResult Execute<TResult>(Expression expression)
        {
            return (TResult) _provider.Execute(expression);
        }
    }
}